﻿using AutoMapper;
using BW.ME.Exam.Domain.Entities;
using BW.ME.Exam.ErrorCode;
using BW.ME.Exam.Infrastructure.Interfaces;
using BW.ME.Exam.Write.Api.Applications.Commands.GClassCommands;
using MediatR;
using System.Transactions;

namespace BW.ME.Exam.Write.Api.Applications.CommandHandlers.GClassCommands
{
    public class FormUpdateCommandHandler : IRequestHandler<FormUpdateCommand, ApiResult<object>>
    {
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="GClassRepository"></param>
        /// <param name="InstanceRepository"></param>
        /// <param name="RecordRepository"></param>
        public FormUpdateCommandHandler( IBaseRepository<WF_Instance> InstanceRepository, IBaseRepository<WF_Record> RecordRepository, IBaseRepository<WF_Setting> SettingRepository)
        {
            this.InstanceRepository = InstanceRepository;
            this.RecordRepository = RecordRepository;
            this.SettingRepository = SettingRepository;
        }
        /// <summary>
        /// 实例仓储
        /// </summary>
        public IBaseRepository<WF_Instance> InstanceRepository { get; }
        /// <summary>
        /// 流转记录仓储
        /// </summary>
        public IBaseRepository<WF_Record> RecordRepository { get; }
        public IBaseRepository<WF_Setting> SettingRepository { get; }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public Task<ApiResult<object>> Handle(FormUpdateCommand request, CancellationToken cancellationToken)
        {
            ApiResult<object> result=new ApiResult<object>();
            result.Code = ResultCode.更新成功;
            result.Message=ResultCode.更新成功.ToString();

            using (TransactionScope scope=new TransactionScope())
            {
                try
                {
                    // 查询当前实例
                    var instance = InstanceRepository.GetAll().FirstOrDefault(x => x.InstanceId == request.InstanceId);
                    var setting=SettingRepository.GetAll().FirstOrDefault(x=>x.PrevNodeNo==instance.NextNodeNo);
                  

                 //流转记录添加
                    WF_Record record =new WF_Record();
                    record.InstanceId=request.InstanceId;
                    record.NodeNo = instance.NextNodeNo;
                    record.NodeName = instance.NextNodeName;
                    record.OperatorId= instance.NextOperatorId;
                    record.OperatorName = instance.NextOperatorName;
                    //record.OperatorOpion = request.ApproveStatus == (int)SPStatus.已通过 ? "已通过" : "未通过";
                    record.OperatorOpion=((SPStatus)request.ApproveStatus).ToString();
                    record.ApproveStatus = request.ApproveStatus;
                    record.ApproveStatusName= ((SPStatus)request.ApproveStatus).ToString();
                    record.CreateTime = DateTime.Now;
                    RecordRepository.Add(record);

                   


                  //实例更新
                    instance.FlowStatus = (int)FlowStatus.处理中;
                    instance.FlowStatusName =FlowStatus.处理中.ToString();

                    if (request.ApproveStatus == (int)SPStatus.未通过)
                    {
                        instance.FlowStatus = (int)FlowStatus.完成;
                        instance.FlowStatusName = FlowStatus.完成.ToString();
                    }

                    instance.CurrentNodeNo = instance.NextNodeNo;
                    instance.CurrentNodeName = instance.NextNodeName;
                    instance.CurrentOperatorId = instance.NextOperatorId;
                    instance.CurrentOperatorName = instance.NextOperatorName;
                    if (setting != null)
                    {
                        instance.NextNodeNo = setting.NodeNo;
                        instance.NextNodeName=setting.NodeName;
                        instance.NextOperatorId = setting.UserId;
                        instance.NextOperatorName = setting.NodeName;
                    }
                    else
                    {
                        instance.NextNodeNo = "";
                        instance.NextNodeName = "";
                        instance.NextOperatorId = 0;
                        instance.NextOperatorName = "";
                        instance.FlowStatus = (int)FlowStatus.完成;
                        instance.FlowStatusName=FlowStatus.完成.ToString() ;
                    }

                    InstanceRepository.Update(instance);
                   


                    //提交事务
                    scope.Complete();
                }
                catch (Exception exp)
                {

                    result.Code = ResultCode.更新失败;
                    result.Message = ResultCode.更新失败.ToString()+exp.Message;
                }
            }



                return Task.FromResult(result);
        }
    }
}
